import { useUserStore } from '../stores/user.js'
// 配置基础地址
const BASE_URL = 'http://localhost:8080/api'

export const request = (options) => {
  // 在本地存储中获取用户的token 添加到请求头中
  const userStore = useUserStore()
  const token = userStore.userInfo.token
  return new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: { token },
      success: (res) => {
        if (res.data.code === -1) {
          uni.showModal({
            title: '错误提示',
            content: res.data.message,
            showCancel: false,
          })
        }
        resolve(res.data)
      },
      fail: (err) => {
        uni.showToast({
          title: '系统错误',
          icon: 'error',
        })
        reject(err)
      },
    })
  })
}
